package com.jlastu.dlxy_androidapp.view.setting;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.contract.ChangePasswordContract;
import com.jlastu.dlxy_androidapp.presenter.ChangePasswordPresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;


public class ChangePasswordActivity extends AppCompatActivity implements ChangePasswordContract.IChangePasswordView {
    private ChangePasswordContract.IChangePasswordPresenter mChangePasswordPresenter;
    private SpUtils mSpUtils;
    private EditText etCurrentPassword, etNewPassword, etConfirmPassword;
    private Button btnChangePassword;

    private int userid;

    private final String TAG = "ChangePasswordActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password_settings);

        mChangePasswordPresenter = new ChangePasswordPresenter(this,this);
        etCurrentPassword = findViewById(R.id.et_current_password);
        etNewPassword = findViewById(R.id.et_new_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
        btnChangePassword = findViewById(R.id.btn_change_password);
        mSpUtils = new SpUtils(getApplicationContext());
        userid = mSpUtils.getInt("userid", 0);

        btnChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    private void changePassword() {
        String currentPassword = etCurrentPassword.getText().toString();
        String newPassword = etNewPassword.getText().toString();
        String confirmPassword = etConfirmPassword.getText().toString();
        if (userid != 0) {
            // 检查密码是否为空
            if (currentPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
                Toast.makeText(this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                return;
            }

            // 检查新密码和确认密码是否匹配
            if (!newPassword.equals(confirmPassword)) {
                Toast.makeText(this, "新密码与确认密码不匹配", Toast.LENGTH_SHORT).show();
                return;
            }
            mChangePasswordPresenter.getUser(userid,currentPassword);
            // 执行密码修改逻辑
            // 显示密码修改成功消息
            // 清空输入框

        }
    }

    @Override
    public void showVerify(int verify) {
        Log.d(TAG, "showVerify: " + verify);
        String confirmPassword = etConfirmPassword.getText().toString();
        if (verify == 1){
            mChangePasswordPresenter.changePassword(userid,confirmPassword);
            Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
            etCurrentPassword.setText("");
            etNewPassword.setText("");
            etConfirmPassword.setText("");
        } else if (verify == 2) {
            Toast.makeText(this, "旧密码错误", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this, "系统错误", Toast.LENGTH_SHORT).show();
        }
    }
}